package com.example.wm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.EmployeeInformationFormTow;

import com.example.wm.mapper.EmployeeInformationFormTowMapper;
import com.example.wm.service.EmployeeInformationFormTowService;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.*;


@Slf4j
@RestController
@RequestMapping("/EmployeeInformation")
public class EmployeeInformationFormTowController {
    @Resource
    private EmployeeInformationFormTowService employeeInformationFormTowService;
    @Resource
    private EmployeeInformationFormTowMapper employeeInformationFormTowMapper;
    @Resource
    private TsyUtils tsyUtils;
    @PostMapping("/insert")
    public Result insert(@RequestBody EmployeeInformationFormTow employeeInformationFormTow){
             employeeInformationFormTow.setInsertdate(tsyUtils.getnowtime());
      int b= employeeInformationFormTowMapper.insert(employeeInformationFormTow);
        return ResultData.success(b);
    }
    @PostMapping("/updatesordelete")
    public Result updates(@RequestBody EmployeeInformationFormTow employeeInformationFormTow){
        employeeInformationFormTow.setUpdatedate(tsyUtils.getnowtime());
        int b=employeeInformationFormTowMapper.updateById(employeeInformationFormTow);
        return ResultData.success(b);
    }

    @PostMapping("/page")
    public Result page(@RequestBody JSONObject jsonObject) {
        Page<EmployeeInformationFormTow> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        LambdaQueryWrapper<EmployeeInformationFormTow> lq=new LambdaQueryWrapper<>();
        lq.eq(EmployeeInformationFormTow::getIsdelete,"false");
//        lq.eq(EmployeeInformationFormTow::getCurrentstatus,"在职");
//        lq.orderByAsc(EmployeeInformationFormTow::getInsertdate);
        lq.eq(EmployeeInformationFormTow::getVenues,jsonObject.getString("cd"));
        Page<EmployeeInformationFormTow>  list=employeeInformationFormTowService.page(pageInfo,lq);
        System.out.println(pageInfo);

        return ResultData.success(pageInfo);
    }

    @PostMapping("/all")
    public Result all() {
        LambdaQueryWrapper<EmployeeInformationFormTow> lq=new LambdaQueryWrapper<>();
        lq.eq(EmployeeInformationFormTow::getIsdelete,"false");
        lq.eq(EmployeeInformationFormTow::getCurrentstatus,"在职");

        Long n=employeeInformationFormTowService.count(lq);



        return ResultData.success(n);
    }

    @PostMapping("/allvenues")
    public Result allvenues() {
        LambdaQueryWrapper<EmployeeInformationFormTow> lq=new LambdaQueryWrapper<>();
        lq.eq(EmployeeInformationFormTow::getIsdelete,"false");
        List<EmployeeInformationFormTow> list =employeeInformationFormTowService.list(lq);
        Set s=new HashSet<>();
        for(EmployeeInformationFormTow e:list){

            s.add(e.getVenues());
        }



        return ResultData.success(s);
    }

//删除
@PostMapping("/delet")
public Result delet(@RequestBody  JSONObject jsonObject) {
    Boolean b=  employeeInformationFormTowService.removeById(jsonObject.getLong("id"));

    return ResultData.success(b);
}
    //批量删除
    @PostMapping("/deletlist")
    public Result deletlist(@RequestBody  JSONObject jsonObject) {
        Boolean b=  employeeInformationFormTowService.removeBatchByIds(jsonObject.getJSONArray("list"));

        return ResultData.success(b);
    }


    @Transactional
    @PostMapping("/readerxlsx")
    public Result inserttmgxid( @RequestBody MultipartFile file) throws IOException {
        // 获取拥有后缀的文件名 「abc.csv」
        String fileName = file.getOriginalFilename(); // 获取文件名
        // 获取后缀 「abc」
        String csvFile = fileName.substring( fileName.indexOf(".")+1);
        System.out.println(csvFile+":cvFilname");
        System.out.println(fileName+"fileNamefileNamefileNamefileName");
        InputStream is = null;
        if(csvFile.equals("xlsx") ||csvFile.equals("xls") ){
            try{

                is = file.getInputStream();
                List<Map> confedrateStaticList = employeeInformationFormTowService.getListByExcel(is,fileName);// 获取解析后的List集合

                System.out.println(confedrateStaticList.toString());
//            Boolean result = confedrateStaticService.batchImportStuInfo(studentList); // 把数据插入数据库
//            if (result){
//                return Msg.sussess().add("info", "文件上传成功！");
//            }else {
//                return Msg.failure().add("info", "文件上传失败！");
//            }
                return ResultData.success(confedrateStaticList);
            }catch (Exception e){
                e.printStackTrace();
            } finally {
                is.close();
            }

        }
        else{
            //  获取 csv 数据

            List<Map> l=   tsyUtils.submitOriginalFile(file);
            return ResultData.success(l);

        }

        return ResultData.success("304", "文件错误！");



    }



}
